﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using System.Xml.Linq;
using System.Linq;


using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;
using System.Collections.Generic;
using ECCIT;
using ECCIT.Web;
public partial class admin_UKNOW_CnEnMapping : AdminBasePage
{
    private const string VIEWSTATE_LAYER_ID = "layerid";
    private const string VIEWSTATE_COLUMN_ID = "columnid";
    private const string VIEWSTATE_TYPE_ID = "typeid";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState[VIEWSTATE_LAYER_ID] = Request.Params[VIEWSTATE_LAYER_ID];
            ViewState[VIEWSTATE_COLUMN_ID] = Request.Params[VIEWSTATE_COLUMN_ID];
            ViewState[VIEWSTATE_TYPE_ID] = Request.Params[VIEWSTATE_TYPE_ID];
            this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Request.Params[""]);
            hdnPopupUrl.Value = GetPopupUrl();
            Grid_viewBild();
        }
        
    }
    private void Grid_viewBild()
    {
        int columnid = int.Parse(Request.Params["columnid"]);
        ColumnModel cm = new ColumnBll().GetColumn(columnid);
        int typeID = int.Parse(Request.Params["typeid"]);
        //DataTable dt = new CnENnMappBLL().CnEnMappData(typeID);
        DataTable dt = CnEnMappingFactory.CreateMappingBLL(1, typeID).GetList();
        //GridView绑定的是一个DataTable对象,不能直接用于分页控件的使用,将其转为List
        var query = (from r in dt.AsEnumerable() select new { COL_MAPPING_ID = r.Field<int>("COL_MAPPING_ID"),
                                                              COL_DATA_ID_CN = r.Field<int>("COL_DATA_ID_CN"),
                                                              COL_EVENTS_TITLE = r.Field<string>("COL_EVENTS_TITLE"),
                                                              COL_EVENTS_TITLE_EN = r.Field<string>("COL_EVENTS_TITLE_EN"),
                                                              COL_DATA_ID_EN = r.Field<int>("COL_DATA_ID_EN"),
        }).Skip(AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex-1)).Take(AspNetPager1.PageSize);
       
        GridView1.DataSource = query.ToList();
        GridView1.DataBind();
        this.AspNetPager1.RecordCount = dt.Rows.Count;
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        //用于分页控件的使用
        this.Grid_viewBild();
    }
    protected void Del_click(object sender, EventArgs e)
    {
        int Cid = int.Parse(((LinkButton)sender).CommandArgument);
        int typeID = int.Parse(Request.Params["typeid"]);
        //int typeID = int.Parse(ViewState[VIEWSTATE_TYPE_ID].ToString());
        bool res = new CnENnMappBLL().delete(Cid,typeID);
        if (res)
        {
            Response.Write("<script>alert('删除成功')</script>");
            Grid_viewBild();
        }
        else
        {
            Response.Write("<script>alert('删除失败')</script>");
        }
    }
    private string GetPopupUrl()
    {
        string url = string.Format("CnEnMapping_Add.aspx?{0}={1}&{2}={3}&{4}={5}",
            VIEWSTATE_COLUMN_ID, ViewState[VIEWSTATE_COLUMN_ID],
            VIEWSTATE_LAYER_ID, ViewState[VIEWSTATE_LAYER_ID],
            VIEWSTATE_TYPE_ID, ViewState[VIEWSTATE_TYPE_ID]);
        return url;
    }

    private string GetRedirectUrl()
    {
        string url = string.Format("CnEnMapping.aspx?{0}={1}&{2}={3}&{4}={5}",
            VIEWSTATE_COLUMN_ID, ViewState[VIEWSTATE_COLUMN_ID],
            VIEWSTATE_LAYER_ID, ViewState[VIEWSTATE_LAYER_ID]);
        return url;
    }
}